package com.example.usercenter.once;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.example.usercenter.mapper.UserMapper;
import com.example.usercenter.model.domain.User;
import com.example.usercenter.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ImportUsers {

    @Resource
    private UserService userService;

    //@Scheduled(fixedDelay = 6000000)
    public void MyTask() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        //List<User> userList = new ArrayList<>();
        //方法一：顺序+批量导入100000用户数据45231ms
//        for (int i = 0; i < 100000; i++) {
//            User user =new User();
//            user.setPlanetCode("111111111");
//            user.setTags("");
//            user.setUserAccount("fakeYuan");
//            user.setUsername("fx1");
//            user.setAvatarUrl("");
//            user.setGender(0);
//            user.setUserPassword("123456789");
//            user.setPhone("");
//            user.setEmail("");
//            user.setUserStatus(0);
//            user.setIsDelete(0);
//            userList.add(user);
//        }
//        userService.saveBatch(userList,10000);
        //方法二：使用异步任务+批量导入导入用户数据4819ms
        List<User> userList = new ArrayList<>();
        CompletableFuture<Void> F1 = CompletableFuture.runAsync(()->{
            for (int i = 0; i < 10000; i++) {
                User user =new User();
                user.setPlanetCode("111111111");
                user.setTags("");
                user.setUserAccount("fakeYuan");
                user.setUsername("fx1");
                user.setAvatarUrl("");
                user.setGender(0);
                user.setUserPassword("123456789");
                user.setPhone("");
                user.setEmail("");
                user.setUserStatus(0);
                user.setIsDelete(0);
                userList.add(user);
            }
            userService.saveBatch(userList,1000);
        });
        //阻塞异步任务
        F1.get();

        long end = System.currentTimeMillis();
        System.out.println("Test时间为："+(end-start));
    }
}
